package tto.gio.next;

import java.util.Calendar;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.Log;




public class StatusManager extends Object {
	class Gio
	{
		public Gio(double a, double b, float c, float d,int e, int f)
		{
			latitude = a;
			longitude = b;
			accuracy = c;
			speed = d;
			cellID = e;
			lac = f;
		}
		public boolean equal(Gio gio)
		{
			if(gio == null)
				return false;
			if(this.latitude == gio.latitude && this.longitude == gio.longitude)
				return true;
			return false;
		}
		double latitude;
		double longitude;
		float speed;
		float accuracy;
		int cellID;
		int lac;
	}
	
	public class Status extends Object{
		Object [][] data;
		double[] loc;
		private String mInsertValue;
		public Status()
		{
			infoLength = MAXDATA;

			data = (Object[][]) new Object[infoLength][]; 
			for(int i=0;i<infoLength;i++)
			{
				data[i] = new Object[MAXDATA];
				//The -1 is unknown value for all segment;
				data[i][1] = -1;
			}
			//prevent type casting error.
			Gio gio = new Gio(-1,-1,-1,-1,-1,-1);
			data[LOC][1] = gio;
			data[NETWORKLOC][0]=gio;
			data[NETWORKLOC][1]=gio;
			data[APP][1] = null;
			Loc = new double[2][2];
		}
		
		public void updateWifi(int state)
		{
			Integer value;
			if(state == WifiManager.WIFI_STATE_ENABLED )
				value = 3;
			else value = WifiManager.WIFI_STATE_DISABLED;
			data[WIFI][0] = (Integer)data[WIFI][1];
			data[WIFI][1] = (Object)value;
		}
		public boolean isChangedWifi()
		{
			boolean res=false;
			
			if((Integer)data[WIFI][0] != (Integer)data[WIFI][1])
				res = true;
			data[WIFI][0] = (Integer)data[WIFI][1];
			return res;
		}
		public String getWifiInfo()
		{
			String str;
			str = WIFI + innerDeter + data[WIFI][1]+deter;
			return str;
		}
		public String getWifiQuery()
		{
			String str;
			str = "WIFI, ";
			mInsertValue = data[WIFI][1]+ ", ";
			return str;
		}
		
		public void updateGPS(Integer state)
		{
			data[GPS][0] = (Integer)data[GPS][1];
			data[GPS][1] = (Object)state;
		}
		public boolean isChangedGPS()
		{
			boolean res=false;
			
			if((Integer)data[GPS][0] != (Integer)data[GPS][1])
				res = true;
			data[GPS][0] = (Integer)data[GPS][1];
			return res;
		}
		public String getGPSInfo()
		{
			String str;
			str = GPS + innerDeter + data[GPS][1]+deter;
			return str;
		}
		public String getGPSQuery()
		{
			String str;
			str = "GPS, ";
			mInsertValue = data[GPS][1]+ ", ";
			return str;
		}
		
		
		public void updateGPRS(Integer state)
		{
			data[GPRS][0] = (Integer)data[GPRS][1];
			data[GPRS][1] = (Object)state;
		}
		public boolean isChangedGPRS()
		{
			boolean res=false;
			
			if((Integer)data[GPRS][0] != (Integer)data[GPRS][1])
				res = true;
			data[GPRS][0] = (Integer)data[GPRS][1];
			return res;
		}
		public String getGPRSInfo()
		{
			//String str;
			//str = GPRS + innerDeter + data[GPRS][1]+deter;
			return null;//str;
		}
		public String getGPRSQuery()
		{
			String str;
			str = "GPRS, ";
			mInsertValue = data[GPRS][1] + ", ";
			return str;
		}
		
		public void updateBrightness(Integer state)
		{
			data[BRIGHTNESS][0] = (Integer)data[BRIGHTNESS][1];
			data[BRIGHTNESS][1] = (Object)state;
		}
		public boolean isChangedBrightness()
		{
			boolean res=false;
			
			if((Integer)data[BRIGHTNESS][0] != (Integer)data[BRIGHTNESS][1])
				res = true;
			data[BRIGHTNESS][0] =(Integer)data[BRIGHTNESS][1];
			return res;
		}
		public String getBrightnessInfo()
		{
			String str;
			str = BRIGHTNESS + innerDeter + (Integer)data[BRIGHTNESS][1]+deter;
			return str;
		}
		public String getBrightnessQuery()
		{
			String str;
			str = "BRIGHTNESS, ";
			mInsertValue = data[BRIGHTNESS][1] + ", ";
			return str;
		}
		
		public void updateBluetooth(Integer state)
		{
			data[BLUETOOTH][0] = (Integer)data[BLUETOOTH][1];
			data[BLUETOOTH][1] = (Object)state;
		}
		public boolean isChangedBluetooth()
		{
			boolean res=false;
			
			if((Integer)data[BLUETOOTH][0] != (Integer)data[BLUETOOTH][1])
				res = true;
			data[BLUETOOTH][0] = (Integer)data[BLUETOOTH][1];
			return res;
		}
		public String getBluetoothInfo()
		{
			String str;
			str = BLUETOOTH + innerDeter + data[BLUETOOTH][1]+deter;
			return str;
		}
		public String getBluetoothQuery()
		{
			String str;
			str = "BLUETOOTH, ";
			mInsertValue = data[BLUETOOTH][1] + ", ";
			return str;
		}
		
		public void updateSound(Integer state)
		{
			data[SOUND][0] = (Integer)data[SOUND][1];
			data[SOUND][1] = (Object)state;
		}
		
		public boolean isChangedSound()
		{
			boolean res=false;
			
			if((Integer)data[SOUND][0] != (Integer)data[SOUND][1])
				res = true;
			data[SOUND][0] = (Integer)data[SOUND][1];
			return res;
		}
		public String getSoundInfo()
		{
			String str;
			str = SOUND + innerDeter + data[SOUND][1]+deter;
			return str;
		}
		public String getSoundQuery()
		{
			String str;
			str = "SOUND, ";
			mInsertValue = data[SOUND][1] + ", ";
			return str;
		}
		public String getDeviceID()
		{
			TelephonyManager tm = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
			String str = "99"+innerDeter+tm.getDeviceId()+deter;
			return str;
		}
		
		
		public void updateMessage(Integer state)
		{
			data[MESSAGE][0] = (Integer)data[MESSAGE][1];
			data[MESSAGE][1] = (Object)state;
		}
		
		public boolean isChangedMessage()
		{
			boolean res=false;
			
			if((Integer)data[MESSAGE][0] == (Integer)data[MESSAGE][1])
				res = true;;
			return res;
		}
		public String getMessageInfo()
		{
			//String str;
			//str = MESSAGE + innerDeter + data[MESSAGE][1]+deter;
			return null;
		}
		public void updateCall(Integer state)
		{
			data[CALL][0] = (Integer)data[CALL][1];
			data[CALL][1] = (Object)state;
		}
		public boolean isChangedCall()
		{
			if((Integer)data[CALL][0] == (Integer)data[CALL][1])
				return true;
			return false;
		}
		public String getCallInfo()
		{
			//String str;
			//str = CALL + innerDeter + data[CALL][1]+deter;
			return null;
		}
		public void updateSensor(Integer state)
		{
			data[SENSOR][0] = (Integer)data[SENSOR][1];
			data[SENSOR][1] = (Object)state;
		}
		public boolean isChangedSensor()
		{
			if((Integer)data[SENSOR][0] == (Integer)data[SENSOR][1])
				return true;
			return false;
		}
		public String getSensorInfo()
		{
			//String str;
			//str = SENSOR + innerDeter + data[SENSOR][1]+deter;
			return null;
		}
		public void updateNetworkLoc(double lat, double lon){
			Gio gio = new Gio(lat,lon, -1,-1,-1,-1);
			data[NETWORKLOC][1] = gio;
		}
		
		public boolean isChangedNetworkLoc()
		{
			boolean res=false;
			Gio gio = (Gio)data[NETWORKLOC][0];
			if(gio.latitude != -1 && gio.longitude != -1)
			{
				res = true;
			}
			data[NETWORKLOC][0] = (Gio)data[NETWORKLOC][1];
			return res;
		}
		public String getNetworkLocInfo()
		{
			
			String str;
			Gio gio = (Gio)data[NETWORKLOC][1];
			str = ""+NETWORKLOC_SENDCODE+ innerDeter + gio.latitude +deter
			+ (NETWORKLOC_SENDCODE+1)+ innerDeter + gio.longitude +deter;
			return  str;
		}
		public String getNetworkLocQuery()
		{
			String str;
			str = "LOC_CELL_LAT, LOC_CELL_LON, ";
			mInsertValue = ((Gio)data[NETWORKLOC][1]).latitude + ", " 
					+ ((Gio)data[NETWORKLOC][1]).longitude + ", ";
			return str;
		}
		
		
		public void updateTime(Integer state)
		{
			//it doesn't necessary
		}
		public void InitState(Context context)
		{
			 mContext = context;
		}
		public boolean isChangedTime()
		{
			return true;	//always embed.
		}
		public String getTimeInfo()
		{			
			String str;
			Calendar cal = Calendar.getInstance();
			
			str = TIME + innerDeter  +cal.get(Calendar.YEAR)+ "-"+
			(cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DATE) + " "
				+ cal.get(Calendar.HOUR_OF_DAY) + ":"+ cal.get(Calendar.MINUTE)
				+ ":"+ cal.get(Calendar.SECOND) + deter;
			Log.d("tto.gio.next","today : "+ str);
			return str;
		}
		public String getTimeQuery()
		{
			String str;
			Calendar cal = Calendar.getInstance();
			str = "TIME ";
			mInsertValue = "'" + cal.get(Calendar.YEAR)+ "-"+
					String.format("%02d",(cal.get(Calendar.MONTH) + 1)) + "-" +
					String.format("%02d",cal.get(Calendar.DATE)) + " "
					+ String.format("%02d",cal.get(Calendar.HOUR_OF_DAY)) + ":"+
					String.format("%02d",cal.get(Calendar.MINUTE))
					+ ":"+ String.format("%02d",cal.get(Calendar.SECOND))+"'" ;
			return str;
		}
		public void updateLocation(double lat, double longitude, float speed, float accurency
				, int cid, int lac)
		{
			Gio gio = new Gio(lat, longitude, speed, accurency, cid, lac);
			data[LOC][0] = (Gio)data[LOC][1];
			data[LOC][1] = (Object)gio;
			
		}
		public boolean isChangedLocation()
		{
			boolean res=false;
			
			if(!((Gio)data[LOC][1]).equal((Gio)data[LOC][0]))
				res = true;
			data[LOC][0] = (Gio)data[LOC][1];
			return res;	//for debug
		}
		public String getLocationInfo()
		{
			String str;
			
			str = LOC + innerDeter + ((Gio)data[LOC][1]).latitude
				+seper+((Gio)data[LOC][1]).longitude+seper
				+((Gio)data[LOC][1]).accuracy +seper + ((Gio)data[LOC][1]).speed
				+seper+((Gio)data[LOC][1]).cellID + seper
				+((Gio)data[LOC][1]).lac +deter
				;
			Log.d("tto.gio.next","location : "+str);
			return str;
		}
		public String getLocationQuery()
		{
			String str;
			str = "LOC_LAT, LOC_LON, ";
			mInsertValue = ((Gio)data[LOC][1]).latitude + ", "
					+ " " + ((Gio)data[LOC][1]).longitude + ", ";
			
			return str;
		}
		
		
		public void updateAPP(String packageName)
		{
			if(data[APP][1] != null)
				data[APP][0] = data[APP][1].toString();
			data[APP][1] = (Object)packageName;
		}
		public boolean isChangedAPP()
		{
			boolean res=false;
			if(data[APP][1] == null)
				return false;
			else if(data[APP][0] == null)
			{
				res = true;
			}
			else if(!data[APP][1].toString().equals(data[APP][0].toString()))
				res = true;
			data[APP][0] = data[APP][1].toString();
			return res;	//for debug
		}
		public String getAPPInfo()
		{
			String str;

			str = APP + innerDeter + (String)data[APP][1]+deter;
			Log.d("tto.gio.next","APP : "+str);
			return str;
		}
		public String getAPPQuery()
		{
			String str;
			str = "APP, ";
			mInsertValue = "'"+ data[APP][1] + "', ";
					
			return str;
		}
		
		public void updateHeadset(Integer state)
		{
			data[HEADSET][0] = (Integer)data[HEADSET][1];
			data[HEADSET][1] = (Object)state;
		}
		public boolean isChangedHeadset()
		{
			boolean res=false;
			
			if((Integer)data[HEADSET][0] != (Integer)data[HEADSET][1])
				res = true;
			data[HEADSET][0] = (Integer)data[HEADSET][1];
			return res;
		}
		public String getHeadsetInfo()
		{
			String str;
			str = HEADSET + innerDeter + data[HEADSET][1]+deter;
			return str;
		}
		public String getHeadsetQuery()
		{
			String str;
			str = "HEADSET, ";
			mInsertValue = data[HEADSET][1] + ", ";
					
			return str;
		}
		public void updatePlugged(Integer state)
		{
			data[PLUGGED][0] = (Integer)data[PLUGGED][1];
			data[PLUGGED][1] = (Object)state;
		}
		public boolean isChangedPlugged()
		{
			boolean res=false;
			
			if((Integer)data[PLUGGED][0] != (Integer)data[PLUGGED][1])
				res = true;
			data[PLUGGED][0] = (Integer)data[PLUGGED][1];
			return res;
		}
		public String getPluggedInfo()
		{
			String str;
			str = PLUGGED + innerDeter + data[PLUGGED][1]+deter;
			return str;
		}
		public String getPluggedQuery()
		{
			String str;
			str = "PLUGGED, ";
			mInsertValue =  data[PLUGGED][1] + ", ";
					
			return str;
		}
		
		
		
		public boolean isChangedStatus(int type)
		{
			switch(type)
			{
			case GPS:
				return isChangedGPS();
			case WIFI:
				return isChangedWifi();
			//case GPRS:
			//	return isChangedGPRS();
			case BRIGHTNESS:
				return isChangedBrightness();
			case BLUETOOTH:
				return isChangedBluetooth();
			case SOUND:
				return isChangedSound();
			//case MESSAGE:
			//	return isChangedMessage();
			//case CALL:
			//	return isChangedCall();
			//case SENSOR:
			//	return isChangedSensor();
			case TIME:
				return isChangedTime();
			case LOC:
				return isChangedLocation();
			case APP:
				return isChangedAPP();
			case HEADSET:
				return isChangedHeadset();
			case PLUGGED:
				return isChangedPlugged();
			case NETWORKLOC:
				return isChangedNetworkLoc();
			
			default:
					return false;
			}
		}
		
		public String getInfo(int type)
		{
			switch(type)
			{
			case GPS:
				return getGPSInfo();
			case WIFI:
				return getWifiInfo();
			//case GPRS:
			//	return getGPRSInfo();
			case BRIGHTNESS:
				return getBrightnessInfo();
			case BLUETOOTH:
				return getBluetoothInfo();
			case SOUND:
				return getSoundInfo();
			//case MESSAGE:
			//	return getMessageInfo();
			//case CALL:
			//	return getCallInfo();
			//case SENSOR:
			//	return getSensorInfo();
			case TIME:
				return getTimeInfo();
			case LOC:
				return getLocationInfo();
			case APP:
				return getAPPInfo();
			case HEADSET:
				return getHeadsetInfo();
			case PLUGGED:
				return getPluggedInfo();
			case NETWORKLOC:
				return getNetworkLocInfo();
				
			default:
					return null;
			}
			
		}
		public String getQuery(int type, StringBuilder values)
		{
			String param;
			switch(type)
			{
			case GPS:
				param = getGPSQuery();
				break;
			case WIFI:
				param = getWifiQuery();
				break;
			case BRIGHTNESS:
				param = getBrightnessQuery();
				break;
			case BLUETOOTH:
				param = getBluetoothQuery();
				break;
			case SOUND:
				param = getSoundQuery();
				break;
			case TIME:
				param =  getTimeQuery();
				break;
			case LOC:
				param =  getLocationQuery();
				break;
			case APP:
				param = getAPPQuery();
				break;
			case HEADSET:
				param =  getHeadsetQuery();
				break;
			case PLUGGED:
				param = getPluggedQuery();
				break;
			case NETWORKLOC:
				param = getNetworkLocQuery();
				break;
			default:
					return null;
			}
			values.append(mInsertValue);
			return param;
		}
		public static final int BRIGHTNESS_SENSOR_ACTION=300;
		public static final int TIME=0;
		public static final int WIFI=1;
		public static final int GPRS=2;
		public static final int BRIGHTNESS=3;
		public static final int BLUETOOTH=4;
		public static final int SOUND=5;
		public static final int MESSAGE=6;
		public static final  int CALL=7;
		public static final int SENSOR=8;
		public static final int GPS = 9;
		public static final int LOC=10;
		public static final int APP=11;
		public static final int HEADSET=12;
		public static final int PLUGGED=13;
		public static final int NETWORKLOC = 14;
		public static final int NETWORKLOC_SENDCODE = 31;
		
		public static final int ID = 99;
		
		public final int MAXDATA=15;
		
		public final int active=1;
		public final int stoped=2;
		
		public final String innerDeter="|";
		public final String deter=";";
		public final String seper = "/";
		
		
		
		public Context mContext;
		
		public double[][] Loc;
		
		int infoLength;
		
	}//endofstatus
	
	

	private StatusManager()
	{
		
		status = new Status();
	}
	
	
	public static StatusManager GetStatusManager()
	{
		if(mStatusManager == null)
			mStatusManager = new StatusManager();
		return mStatusManager;
	}
	
	public boolean UpdateState(int type, Intent intent)
	{
		return true;
	}
	
	
	protected static StatusManager mStatusManager;
	public Status status;
	
}
